<?php
/**
 * Created by PhpStorm.
 * User: dung
 * Date: 3/26/14
 * Time: 10:09 AM
 */
//namespace Admin\Models;


class Admin_Model_Category extends \Zend_Db_Table
{
    protected $_name = 'film_cat';
    protected $_primary = 'cat_id';

    /**
     * @param null $arrParams
     * @param null $options
     * @return mixed
     */
    public function countItem($arrParams = null, $options = null)
    {
        $db = Zend_Registry::get('connectDb');
        $select = $db->select()->from('film_cat as c', array('COUNT(cat_id) as totalItem'));
        if (isset($arrParams['conditions']) && !empty($arrParams['conditions'])) {
            $conditions = $arrParams['conditions'];
            foreach ($conditions as $condition) {
                $select->where($condition['key'], $condition['exp'], $condition['value']);
            }
        }
        $result = $db->fetchOne($select);
        return $result;
    }

    /**
     * @param null $data
     * @param null $options
     * @return mixed
     */
    public function saveItem($data = null, $options = null)
    {
        $row = (!isset($data['Category']['cat_id']) && empty($data['Category']['cat_id'])) ? $this->fetchNew() : $this->fetchRow('cat_id = ' . $data['Category']['cat_id']);

        foreach ($data['Category'] as $key => $value) {
            if ($key !== 'cat_id')
                $row->{$key} = $value;
        }
        $row->save();
        return $row->cat_id;
    }

    /**
     * @param null $data
     * @param null $options
     * @return array
     */
    public function getItem($data = null, $options = null)
    {
        if ($options['task'] == 'admin-info' || $options['task'] == 'admin-edit') {
            $where = "cat_id = " . $data['id'];
            $result = $this->fetchRow($where)->toArray();
        }
        return $result;
    }

    public function getItemInSelectBox($data = null, $options = null)
    {

        $db = Zend_Registry::get('connectDb');
        $select = $db->select()->from('film_cat',array('cat_id as id','cat_name as title','sub_id as parent_id'))
                ->order('sub_id ASC');
        $result = $db->fetchAll($select);
        return $result;
    }
}